package com.bluesky.javawebbrowser.domain.html.tags.form.input;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JTextField;

import com.bluesky.javawebbrowser.domain.html.tags.Tag;
import com.bluesky.javawebbrowser.domain.html.tags.TagType;
import com.bluesky.javawebbrowser.domain.html.tags.form.FormElement;

public class Input extends Tag implements FormElement {

	protected InputType inputType; // %InputType; TEXT -- what kind of widget is
	// needed
	// --
	private String name; // CDATA #IMPLIED -- submit as part of form --
	private String value; // CDATA #IMPLIED -- Specify for radio buttons and
	// checkboxes --
	private boolean checked; // (checked) #IMPLIED -- for radio buttons and
	// check
	// boxes --
	private boolean disabled;// (disabled) #IMPLIED -- unavailable in this
	// context --
	private boolean readonly;// (readonly) #IMPLIED -- for text and passwd --
	private String size;// CDATA #IMPLIED -- specific to each type of field --
	private int maxlength;// NUMBER #IMPLIED -- max chars for text fields --
	private String src;// %URI; #IMPLIED -- for fields with images --
	private String alt;// CDATA #IMPLIED -- short description --
	private String usemap;// %URI; #IMPLIED -- use client-side image map --
	private boolean ismap;// (ismap) #IMPLIED -- use server-side image map --
	private int tabindex;// NUMBER #IMPLIED -- position in tabbing order --
	private char accesskey;// %Character; #IMPLIED -- accessibility key
	// character --
	private String onfocus;// %Script; #IMPLIED -- the element got the focus --
	private String onblur;// %Script; #IMPLIED -- the element lost the focus --
	private String onselect;// %Script; #IMPLIED -- some text was selected --
	private String onchange;// %Script; #IMPLIED -- the element value was
	// changed --
	private String accept;// %ContentTypes; #IMPLIED -- list of MIME types for
	// file upload --
	private String align;// %IAlign; #IMPLIED -- vertical or horizontal

	// alignment --

	// events --
	protected String onClick;// %Script; #IMPLIED -- a pointer button was
	// clicked --
	protected String onBblClick;// %Script; #IMPLIED -- a pointer button was
	// double clicked--
	protected String onMouseDown;// %Script; #IMPLIED -- a pointer button was
	// pressed down --
	protected String onMouseUp;// %Script; #IMPLIED -- a pointer button was
	// released --
	protected String onMouseOver;// %Script; #IMPLIED -- a pointer was moved
	// onto --
	protected String onMouseMove;// %Script; #IMPLIED -- a pointer was moved
	// within --
	protected String onMouseOut;// %Script; #IMPLIED -- a pointer was moved away
	// --
	protected String onKeyPress;// %Script; #IMPLIED -- a key was pressed and
	// released --
	protected String onKeyDown;// %Script; #IMPLIED -- a key was pressed down --
	protected String onKeyUp;// %Script; #IMPLIED -- a key was released --"

	public Input() {
		super(TagType.INPUT);

	}

	public Input(Tag tag) {
		super(TagType.INPUT);

		copy(tag);

		attributes = tag.getAttributes();

		String sType = (String) tag.getAttribute("type");
		if (sType == null)
			sType = "text";
		inputType = InputType.valueOf(sType.toUpperCase());

		name = (String) tag.getAttribute("name");
		value = (String) tag.getAttribute("value");

		checked = tag.getAttributes().containsKey("checked");

	}

	public InputType getType() {
		return inputType;
	}

	public void setType(InputType type) {
		this.inputType = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
		attributes.put("name", name);
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
		attributes.put("value", value);
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {

		this.checked = checked;

		if (checked)
			attributes.put("checked", null);
		else
			attributes.remove("checked");
	}

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;

		if (disabled)
			setAttribute("disabled", "disabled");
		else
			removeAttribute("disabled");
	}

	public boolean isReadonly() {
		return readonly;
	}

	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
		
		setAttribute("size", size,true);
	}

	public int getMaxlength() {
		return maxlength;
	}

	public void setMaxlength(int maxlength) {
		this.maxlength = maxlength;
	}

	public String getSrc() {
		return src;
	}

	public void setSrc(String src) {
		this.src = src;
	}

	public String getAlt() {
		return alt;
	}

	public void setAlt(String alt) {
		this.alt = alt;
	}

	public String getUsemap() {
		return usemap;
	}

	public void setUsemap(String usemap) {
		this.usemap = usemap;
	}

	public boolean isIsmap() {
		return ismap;
	}

	public void setIsmap(boolean ismap) {
		this.ismap = ismap;
	}

	public int getTabindex() {
		return tabindex;
	}

	public void setTabindex(int tabindex) {
		this.tabindex = tabindex;
	}

	public char getAccesskey() {
		return accesskey;
	}

	public void setAccesskey(char accesskey) {
		this.accesskey = accesskey;
	}

	public String getOnfocus() {
		return onfocus;
	}

	public void setOnfocus(String onfocus) {
		this.onfocus = onfocus;
	}

	public String getOnblur() {
		return onblur;
	}

	public void setOnblur(String onblur) {
		this.onblur = onblur;
	}

	public String getOnselect() {
		return onselect;
	}

	public void setOnselect(String onselect) {
		this.onselect = onselect;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getAccept() {
		return accept;
	}

	public void setAccept(String accept) {
		this.accept = accept;
	}

	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public String getOnClick() {
		return onClick;
	}

	public void setOnClick(String onClick) {
		this.onClick = onClick;
		getAttributes().put("onClick", onClick);
	}

	public String getOnBblClick() {
		return onBblClick;
	}

	public void setOnBblClick(String onBblClick) {
		this.onBblClick = onBblClick;
	}

	public String getOnMouseDown() {
		return onMouseDown;
	}

	public void setOnMouseDown(String onMouseDown) {
		this.onMouseDown = onMouseDown;
	}

	public String getOnMouseUp() {
		return onMouseUp;
	}

	public void setOnMouseUp(String onMouseUp) {
		this.onMouseUp = onMouseUp;
	}

	public String getOnMouseOver() {
		return onMouseOver;
	}

	public void setOnMouseOver(String onMouseOver) {
		this.onMouseOver = onMouseOver;
	}

	public String getOnMouseMove() {
		return onMouseMove;
	}

	public void setOnMouseMove(String onMouseMove) {
		this.onMouseMove = onMouseMove;
	}

	public String getOnMouseOut() {
		return onMouseOut;
	}

	public void setOnMouseOut(String onMouseOut) {
		this.onMouseOut = onMouseOut;
	}

	public String getOnKeyPress() {
		return onKeyPress;
	}

	public void setOnKeyPress(String onKeyPress) {
		this.onKeyPress = onKeyPress;
	}

	public String getOnKeyDown() {
		return onKeyDown;
	}

	public void setOnKeyDown(String onKeyDown) {
		this.onKeyDown = onKeyDown;
	}

	public String getOnKeyUp() {
		return onKeyUp;
	}

	public void setOnKeyUp(String onKeyUp) {
		this.onKeyUp = onKeyUp;
	}

	public InputType getInputType() {
		return inputType;
	}

	public void setInputType(InputType inputType) {
		this.inputType = inputType;
		setAttribute("type", inputType.name());
	}

	/**
	 * a input tag has no child.
	 */
	@Override
	public String toHtml() {

		StringBuilder sb = new StringBuilder();

		sb.append("\n" + Tag.indents(getDepth(), "  ") + getHEAD());

		// Iterator<TreeNode> it = getChildren().iterator();
		// while (it.hasNext()) {
		// Tag tag = (Tag) it.next();
		// sb.append(tag.toHtml());
		// }

		// sb.append("\n" + Tag.indents(getDepth(), "  ") + getFOOT());

		return sb.toString();

	}

	@Override
	public JComponent getComponent() {
		JComponent comp = null;

		if (getType().equals(InputType.BUTTON))
			comp = new JButton(getValue());
		else if (getType().equals(InputType.SUBMIT))
			comp = new JButton(getValue());
		else if (getType().equals(InputType.TEXT))
			comp = new JTextField(getValue());
		else
			comp = new JTextField(getValue());

		return comp;
	}

	@Override
	public String getHttpValue() {
		return getValue();

	}

	@Override
	public void setHttpValue(String value) {
		setValue(value);
	}
}
